﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;
using WebSite.Core;

namespace WebSite.Server
{
    /// <summary>
    /// InviteTeam 的摘要说明
    /// </summary>
    public class InviteTeam : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest( HttpContext context )
        {
            context.Response.ContentType = "text/json";

            RetInfo response = RetInfoHelper.WebServiceReturn( delegate()
            {
                int targetuid = StringHelper.Convert( context.Request["targetuid"], 0 );

                if (Project_Session.CurrentUser == null || Project_Session.CurrentUserBaby == null)
                {
                    throw new RuntimeException( 2, "请先完善宝宝资料" );
                }

                if (NullableHelper.CastValueType( Project_Session.CurrentUserBaby.ConfirmTeamId ) != 0)
                {
                    throw new RuntimeException( 10, "您的宝宝已经组队" );
                }

                if (targetuid == 0 )
                {
                    throw new RuntimeException( 11, "无效的目标宝宝" );
                    // return RetInfo.DefaultError( "无效的用户标识" );
                }

                if (Project_Session.CurrentUser.UserId == targetuid)
                {
                    throw new RuntimeException( 12, "不能与自己组队" );
                }
                

                Maticsoft.BLL.CarrefourCover.Baby babybll = new Maticsoft.BLL.CarrefourCover.Baby();
                var babymodel = babybll.GetModel( targetuid );
                if (babymodel == null || NullableHelper.CastValueType( babymodel.ConfirmTeamId ) != 0)
                {
                    throw new RuntimeException( 22, "对方宝宝信息不存在或已经组队" );
                }

                Maticsoft.BLL.CarrefourCover.ShortMessage shortmessagebll = new Maticsoft.BLL.CarrefourCover.ShortMessage();
                // 验证本日邀请次数
                if (shortmessagebll.getTodaySendCount( Project_Session.CurrentUserID ) >= 5)
                {
                    throw new RuntimeException( 22, "每天最多只能邀请5次" );
                }
                
                shortmessagebll.Add( new Maticsoft.Model.CarrefourCover.ShortMessage()
                {
                    RecevieBabyId = targetuid,
                    SendBabyId = Project_Session.CurrentUser.UserId,
                    SendBabyName = Project_Session.CurrentUserBaby.BabyName,
                    SendBabyHeadImg = Project_Session.CurrentUserBaby.BabyHeadImg,
                    MsgType = 1,
                    MsgContent = "我想要和你一起组成宝宝帮参赛",
                    IsRead = 0,
                    IsDelete = 0,
                    CreateTime = DateTime.Now,
                    Ip = context.Request.UserHostAddress
                } );

                return true;
            } );

            context.Response.Write( response.SerializeToString( Common.Serialization.SerializationType.Json ) );
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}